package com.clz.appdesign.myapp;

import android.Manifest;
import android.content.Context;
import android.os.Environment;
import android.widget.ProgressBar;

import com.blankj.utilcode.util.ToastUtils;
import com.clz.appdesign.appdependence.base.BaseActivity;
import com.hengda.zwf.httputil.download.RxDownload;
import com.hengda.zwf.httputil.download.entity.DownloadStatus;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public class RxDownLoadActivity extends BaseActivity {

    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;
    private RxPermissions mRxPermissions;

    @Override
    public void initToolbar() {
        ButterKnife.bind(this);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        mRxPermissions = new RxPermissions(this);
    }

    @Override
    public void doBusiness(Context mContext) {

    }

    @Override
    public int layoutResID() {
        return R.layout.activity_rx_down_load;
    }

    @OnClick(R.id.btn_download)
    public void onViewClicked() {
        mRxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean isAllow) throws Exception {
                        if (isAllow) {
                            downLoadFile();
                        } else {
                            ToastUtils.showShort("请打开读取内存卡权限");
                        }
                    }
                });
    }

    private void downLoadFile() {
        String url = "http://imtt.dd.qq.com/16891/6F77AEDEDB4F0E5728545F3AA8E0A1CB.apk?fsname=com.tencent.weishi_4.3.0.88_430.apk&amp;csr=1bbd";
        String fileName = "MyApp.apk";
        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + File.separator + getPackageName() + File.separator;
        RxDownload.getInstance()
                .maxThread(4).maxRetryCount(3)
                .download(url, fileName, filePath)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {

                    }
                })
                .doOnNext(new Consumer<DownloadStatus>() {
                    @Override
                    public void accept(DownloadStatus downloadStatus) throws Exception {
                        //此处更新下载进度
                        int process = 0;
                        if (downloadStatus.getTotalSize() != 0L) {
                            process = (int) (downloadStatus.getDownloadSize() * 100 / downloadStatus.getTotalSize());
                        }
                        mProgressBar.setProgress(process);
                    }
                })
                .doOnError(new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showShort("下载异常");
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {

                    }
                });
    }
}
